import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/refresh_util.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:module_business/comm/service/comm_service.dart';

class ListViewModel extends BaseViewModel {
  CommService commService = CommService();
  int page = 1;
  bool isCanMoreData = true;
  String keyword = '';
  dynamic total = 0;
  CommRefreshController controller = CommRefreshController();

  List<int> dataLists = List.generate(20, (index) => index);

  @override
  void onInit() {
    super.onInit();

    onRefresh();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  onRefresh({
    int? pageCur,
  }) async {
    if (pageCur != null) page = pageCur;
    await Future.delayed(Duration(seconds: 2));
    if (page == 1) {
      dataLists = List.generate(10, (index) => index);
    } else {
      dataLists.addAll(List.generate(10, (index) => dataLists.length + index));
    }
    LogUtil.d('onRefresh-->dataLists=${dataLists.map((e) => e.toString()).toList()}');
    controller.setOperStatus(curPage: page, existDataLength: 50, curDataLength: page>5?0:10);
    update();
  }
}
